package com.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/admin/cookie04.do")
public class Cookie04Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //修改Cookie的第二种方式
        //直接new 同名的Cookie 然后发送到浏览器
        Cookie cookie04 = new Cookie("cookie04", "value1234");
        cookie04.setPath(request.getContextPath() + "/admin");
        //2.通知客户端保存Cookie
        response.addCookie(cookie04);
        //获取Cookie对象
        Cookie[] cookies = request.getCookies();
        for (int i = 0; i < cookies.length; i++) {
            Cookie cookie = cookies[i];
            System.out.println("cookie的名称：" + cookie.getName());
            System.out.println("cookie的值：" + cookie.getValue());
            System.out.println("cookie的寿命：" + cookie.getMaxAge());
            System.out.println("cookie的域名：" + cookie.getDomain());
            System.out.println("cookie的URI：" + cookie.getPath());
            System.out.println("cookie的安全等级：" + cookie.getSecure());
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
